package main

import (
	"fmt"
	"sync"
	"time"
)

var moneyChan1 = make(chan int)
var nameChan1 = make(chan string)
var doneChan1 = make(chan struct{})

func send1(name string, money int, wait *sync.WaitGroup) {
	fmt.Println(name, "来购物")
	time.Sleep(1 * time.Second)
	//将钱放到信道中
	moneyChan1 <- money
	nameChan1 <- name
	fmt.Println(name, "购物结束")
	wait.Done()
}

func main() {
	var wait sync.WaitGroup
	var now = time.Now()
	wait.Add(3)
	go send1("张三", 2, &wait)
	go send1("里斯", 4, &wait)
	go send1("王五", 5, &wait)
	go func() {
		wait.Wait()
		close(moneyChan1)
		close(nameChan1)
		close(doneChan1)
	}()
	var moneyList []int
	var nameList []string

	for {
		select {
		case money := <-moneyChan1:
			moneyList = append(moneyList, money)
		case name := <-nameChan1:
			nameList = append(nameList, name)
		case <-doneChan1:
			fmt.Println("总耗时", time.Since(now))
			fmt.Println("资金集合", moneyList)
			fmt.Println("姓名集合", nameList)
			fmt.Println("结束")
			return
		}
	}
	//go func() {
	//	for money := range moneyChan1 {
	//		moneyList = append(moneyList, money)
	//	}
	//}()
	//
	//for name := range nameChan1 {
	//	nameList = append(nameList, name)
	//}
}
